#
# Common functions
#

# ------------------------------------------------------------------------------
# Verify passwords
# Usage:
#       check_passwords $input1 $input2
# ------------------------------------------------------------------------------
function check_passwords() {
    if [ $# -ne "2" ]; then
        echo "No passwords provided. Nothing to do."
        return 1
    fi
    
    local _p1=$1
    local _p2=$2
    if [ ! "$_p1" = "$_p2" ]; then
        echo "Passwords do not match"
        return 1
    fi
    
    return 0
}

# ------------------------------------------------------------------------------
# Prompt for password input and store it in the first parameter.
# Usage:
#       get_password INPUT
#       echo "Password is: $INPUT"
# ------------------------------------------------------------------------------
function get_password() {
    local _password=$1
    local _input1=
    local _input2=
    local _verified=1
    
    while [ ! $_verified -eq 0 ]; do
        read -p "Enter password: " -s _input1
        echo ""
        read -p "Re-enter password: " -s _input2
        echo ""
        check_passwords $_input1 $_input2
        _verified=$?
        if [ $_verified -eq 0 ]; then
            eval $_password="'$_input1'"
        fi
    done
}

# ------------------------------------------------------------------------------
# Check to see if the user is root and exit 1 if not.
# Usage:ZZZZZZZZZZ_SERVERNAME_ZZZZZZZZZZ
#       isRoot
#       isRoot "Personalized exit message"
# ------------------------------------------------------------------------------
function isRoot() {
    local _msg="You must be root user"
    if [ $# -eq 1 ]; then
        _msg="$1"
    fi

    if [[ $EUID -ne 0 ]]; then
        echo $_msg 2>&1
        exit 1
    fi
}

# ------------------------------------------------------------------------------
# Pause with a message
# ------------------------------------------------------------------------------
function pause() {
    read -p "$*"
}

# ------------------------------------------------------------------------------
# Check if user exists
# 0 = exists
# 1 = does not exist
# ------------------------------------------------------------------------------
function user_exists() {
    USERID=$1
    grep  -i "^${USERID}" /etc/passwd
    if [ $? -eq 0 ]; then
        echo "User $USERID exists in /etc/passwd"
        return 0
    else
        echo "User $USERID does not exists in /etc/passwd"
        return 1
    fi
}

# ------------------------------------------------------------------------------
# Check if group exists
# 0 = exists
# 1 = does not exist
# ------------------------------------------------------------------------------
function group_exists() {
    GROUPID=$1
    grep  -i "^${GROUPID}" /etc/group
    if [ $? -eq 0 ]; then
        echo "Group $GROUPID exists in /etc/group"
        return 0
    else
        echo "Group $GROUPID does not exists in /etc/group"
        return 1
    fi
}

# ------------------------------------------------------------------------------
# Backup a file/directory
# 0 = success
# 1 = error
# ------------------------------------------------------------------------------
function backup() {
    EXPECTED_ARGS=1
    DATE=(`date +"%Y%m%d%H%M"`)
    
    if [ ! $# -eq $EXPECTED_ARGS ]
    then
        echo "Usage: $0 file|dir"
        return 1
    fi
    
    if [ $# -eq 0 ]
    then
        echo "Nothing to backup"
        return 1
    else
        ITEM=$1
        echo "Backup: cp $ITEM $ITEM.bk.$DATE"
        cp $ITEM $ITEM.bk.$DATE > /dev/null 2>&1
        return $?
    fi
}

# ------------------------------------------------------------------------------
# Prompt for a message
# Usage:
#     - prompt : pause routine
#     - prompt <message> : prompts with a specified message
#     - prompt <input> <message> : prompts with the 
#           specified message and store the user input in
#           the first parameter, input.
#     - prompt <input> <message> <regex> : same as above
#            but will loop until the user inputs a value 
#            that matches the provided regex
# ------------------------------------------------------------------------------
function prompt() {
    local __resultvar=
    local __message=
    local __regex=
    local __input=

    local __NUM_ARGS=$#
    case $__NUM_ARGS in
        1)
            __message=$1
            pause "$__message"
            ;;
        2)
            __resultvar=$1
            __message=$2
            read -p "$__message" __input
            eval $__resultvar="'$__input'"
            ;;
        3)
            __resultvar=$1
            __message=$2
            __regex=$3
            while ! [[ $__input =~ $__regex ]]; do
                read -p "$__message" __input
                eval $__resultvar="'$__input'"
#                if [[ $__input =~ ^$ ]]; then
#                    echo "Match: $__input"
#                    eval $__resultvar="'$__input'"
#                fi
            done
            ;;
        *)
            __message="Press [ENTER] to continue"
            pause "$__message"
            ;;
    esac
}

# ------------------------------------------------------------------------------
# Open the specified parameter port.
# - $1: port number
# 0 = success
# 1 = error
# ------------------------------------------------------------------------------
function open_port() {
    EXPECTED_ARGS=1
    if [ ! $# -eq $EXPECTED_ARGS ];
    then
        return 1
    fi
    
    PORT=$1
    
    echo "Modify iptables to access incoming traffic on port $PORT"
    iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport $PORT -j ACCEPT
    iptables-save > iptables.new
    
    echo "Backing up /etc/sysconfig/iptables"
    backup /etc/sysconfig/iptables
    if [ ! $? -eq 0 ];
    then
        echo "Error: Encountered error during backup of /etc/sysconfig/iptables"
        return 1
    fi
    
    rm -Rf /etc/sysconfig/iptables
    mv iptables.new /etc/sysconfig/iptables
    service iptables restart
    return 0
}

# ------------------------------------------------------------------------------
# Move all files in the specified directory to the specified
# destination directory.
# - $1: source directory name
# - $2: destination directory name
# 0 = success
# 1 = error
# ------------------------------------------------------------------------------
function move_files() {
    DIR_NAME=$1
    DESTINATION=$2
    for script in $DIR_NAME
    do
        echo "Moving $DIR_NAME/$script to $DESINTATION"
        mv -f $DIR_NAME/$script $DESINTATION
        if [ ! $? -eq 0 ];
        then
            echo "Error: Cannot move $DIR_NAME/$script $DESINTATION"
            return 1
        fi
    done
    return 0
}

# ------------------------------------------------------------------------------
# Export functions to make them public
# ------------------------------------------------------------------------------
#export -f pause
#export -f print_env
#export -f check_mysql
#export -f check_passwords
#export -f isRoot
